//引入工具类
const util = require('../../utils/util');
// 加载配置文件
const config = require('../../utils/config');

const app = getApp()

Page({
  data: {
    array: ['正常', '异常', '停用', '未启用'],
    statusList: [{
        id: 0,
        name: '正常'
      },
      {
        id: 1,
        name: '异常'
      },
      {
        id: 2,
        name: '停用'
      }, {
        id: 3,
        name: '未启用'
      }
    ],
    state: {
      0: '正常',
      1: '异常',
      2: '停用',
      3: '未启用'
    },
    stateColor: {
      0: 'green',
      1: 'red',
      2: 'yellow',
      3: 'default'
    },
    statusIndex: '',
    listData: [],
    pageTitle: '',
    listTitle: '',
    page: 1,
    searVal: '',
    lastPage: false,
    flag: '',
    innerCode: '',
    meterAddress: '',
    sortFlag: '',
    isLoadMore: true
  },
  bindPickerChange(e) {
    this.setData({
      statusIndex: e.detail.value
    })
    this.resetLoadData()
  },
  handleSort() {
    this.setData({
      sortFlag: this.data.sortFlag == '' || this.data.sortFlag == 'asc' ? 'desc' : 'asc'
    })
    this.resetLoadData()
  },
  changeSearVal(e) {
    this.setData({
      searVal: e.detail
    })
  },
  onSearch() {
    this.resetLoadData();
  },
  getListData() {
    wx.showLoading({
      title: '加载中...'
    })
    this.setData({
      isLoadMore: false
    });
    var apiUrl = 'company/findList';
    var params = {
      keyword: this.data.searVal,
      page: this.data.page,
      rows: 20
    }
    if (this.data.flag == 'true') {
      apiUrl = 'meter/findMeterList';
      if (this.data.pageTitle == "实时数据") {
        apiUrl = 'actual/findActualList';
        params.status = this.data.statusIndex
        params.sord = this.data.sortFlag
        params.sidx = !!this.data.sortFlag ? 'sum_water' : ''
      }
      params.innerCode = this.data.innerCode
    }
    let _this = this;
    app.ajax({
      url: `${config.httpUrl}${apiUrl}`,
      method: 'get',
      data: params,
      callback: function(error, data) {
        wx.hideLoading();
        _this.setData({
          page: _this.data.page + 1,
          lastPage: data.data.lastPage,
          listData: _this.data.listData.concat(data.data.list),
          isLoadMore: true
        });
      }
    })
  },
  goDetail(e) {
    let id = e.currentTarget.dataset.id
    let url = `/pages/list/list?name=${this.data.pageTitle}&flag=true&inner_code=${e.currentTarget.dataset.code}&title=${e.currentTarget.dataset.title}`
    if (this.data.pageTitle == '单位查询') {
      url = `/pages/detail/detail?name=${this.data.pageTitle}&id=${id}`
    }

    if (this.data.flag == 'true' && (this.data.pageTitle == '实时数据' || this.data.pageTitle == '水表查询')) {
      url = `/pages/detail/detail?name=${this.data.pageTitle}&id=${id}`
      if (this.data.pageTitle == '实时数据') {
        url = `/pages/detail/detail?name=${this.data.pageTitle}&id=${id}&state=${e.currentTarget.dataset.state}`
      }
    }

    if (this.data.flag == 'true' && this.data.pageTitle == '读数查询') {
      url = `/pages/read-line/read-line?name=${this.data.pageTitle}&meterAddress=${e.currentTarget.dataset.meteraddress}&title=${e.currentTarget.dataset.title}`
    }
    wx.navigateTo({
      url
    });
  },

  onLoad: function(options) {
    var _this = this;
    _this.setData({
      pageTitle: options.name,
      listTitle: options.title || '',
      flag: options.flag || '',
      id: options.id || '',
      innerCode: options.inner_code || '',
      meterAddress: options.meterAddress || ''
    })
    wx.setNavigationBarTitle({
      title: _this.data.pageTitle
    })
    this.getListData()
  },
  resetLoadData() {
    this.setData({
      page: 1,
      listData: [],
      isLoadMore: true
    })
    this.getListData();
  },

  onPullDownRefresh: function() {
    this.setData({
      searVal: ''
    })
    this.resetLoadData();
    wx.stopPullDownRefresh()
  },

  onReachBottom: function() {
    if (!this.data.lastPage && this.data.isLoadMore) {
      this.getListData();
    }
  },
  onShow() {
    let pages = getCurrentPages();
    let curPage = pages[pages.length - 1];
    app.checkBind(curPage.route);
  }
})